package merchantCard

import (
	"errors"
	"fmt"
	wxcard "github.com/chanxuehong/wechat/mp/card"
)

type CARDMODE string

const (
	CARD_MODE_MCH        = "CARD_MODE_MCH"        //仅支持商户卡
	CARD_MODE_MCH_WECHAT = "CARD_MODE_MCH_WECHAT" //同时支持商户卡和微信卡，既，商户会员卡具备同步到微信会员卡的能力
	CARD_MODE_WECHAT     = "CARD_MODE_WECHAT"     //同时支持商户卡和微信卡，既，商户会员卡具备同步到微信会员卡的能力
)

const (
	MEMBER_LIST_ORDER_FEILD_NULL = "" //无，默认排序
	//最近到店、会员等级、优质会员、近30天生日、30天未来
	MEMBER_LIST_ORDER_FEILD_RENENTLY_ARRIVED = "recently_arrived_to_shop" //最近到店
	MEMBER_LIST_ORDER_FEILD_MEMBER_LEVEL_NO = "member_level_no" //会员等级
	MEMBER_LIST_ORDER_FEILD_PREMIUM_MEMBER = "premium_member" //优质会员
	MEMBER_LIST_ORDER_FEILD_BIRTHDAY_IN_30_DAYS = "birthday_in_30_days" //近30天生日
	MEMBER_LIST_ORDER_FEILD_NOT_ARRIVED_IN_30_DAYS = "not_arrived_in_30_days" //近30天生日
)

type MchCardConf struct {
	MchCardBaseInfo MchCardBaseInfo `json:"mch_card_base_info" xorm:"card_mode"`
	//MchCardId int64 `json:"mch_card_id" xorm:"mch_card_id"`
	//MchMemberCard *MchMemberCard `json:"mch_member_card,omitempty"`
	MchMemberCardConf *MchMemberCardConf `json:"mch_member_card_conf" xorm:""`
}
type MchCardBaseInfo struct {

}

type MchMemberCardConf struct {
	MemberLevelConf []MemberLevelConf `json:"member_level_conf" xorm:""`
}

//会员门槛，成为会员的条件
type MemberCondition struct {
	//消费金额 满__元
	ConsumeAmount int `json:"consume_amount"`

	//余额充值 满__元
	RechargeAmount int `json:"recharge_amount"`

	//直推人数 满__人
	DirectPushNum int `json:"direct_push_num"`

}

//会员特价折扣日
type MemberDiscountDay struct {
	DayInWeek           int     `json:"day_in_week"`
	DayInMonth          int     `json:"day_in_month"` //0或负数表示无折扣日
	DiscountInMemberDay float32 `json:"discount_in_member_day"`
}

type MemberLevelConf struct {
	LevelNo int `json:"level_no"`
	LogoURL     string    `json:"logo_url,omitempty"`
	BackgoundPicUrl   string `json:"background_pic_url,omitempty"`
	MemberLevelName   string            `json:"member_level_name"`
	MemberCond        MemberCondition   `json:"member_cond"`
	MemberDiscount    float32           `json:"member_discount"`
	MemberDiscountDay MemberDiscountDay `json:"member_discount_day"`
	CommissionLvl1    float32           `json:"commision_lvl_1"`
	CommissionLvl2    float32           `json:"commision_lvl_2"`
}



type MchMemberCardAttr struct {
	//MchCardBaseInfo MchCardBaseInfo `json:"mch_card_base_info"`  //mch card的子类都需要继承 MchCardBaseInfo
	MemberLevelConf []*MemberLevelConf `json:"member_level_conf" xorm:""`
}


func CheckMchCardMsg(mchCardMsg *MchCardUnionMsg) error {
	/*if mchCardMsg.MchCardConf == nil {
		return errors.New("parameter mch_card_attr is missing")
	}*/



	if mchCardMsg.Card.CardType == wxcard.CardTypeMemberCard && mchCardMsg.MchMemberCard == nil {
		return errors.New("parameter mch_member_card_conf is invalid")
	}

	if mchCardMsg.Card.CardType == wxcard.CardTypeMemberCard {
		if mchCardMsg.CardMode != CARD_MODE_MCH &&
			mchCardMsg.CardMode != CARD_MODE_MCH_WECHAT &&
			mchCardMsg.CardMode != CARD_MODE_WECHAT {
			return errors.New("parameter card_mode is invalid")
		}
	} else {
		return fmt.Errorf("not support card type<%s>", mchCardMsg.Card.CardType)
	}


	return nil
}
